package top.toptimus.dto.material;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import top.toptimus.util.ValueUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Map;

/**
 * 物料全部基础信息库存DTO
 *
 * @author gaoyu
 */
@NoArgsConstructor
@AllArgsConstructor
public class MaterialAllInfoInventoryDTO extends MaterialAllInfoDTO {
    @Getter
    protected double fabricQuanlity;  //  库存数量
    @Getter
    protected double unitPrice;   //  库存加权平均价格

    public MaterialAllInfoInventoryDTO build(double fabricQuanlity) {
        this.fabricQuanlity = fabricQuanlity;
        return this;
    }

    /**
     * 清空库存数
     *
     * @return this
     */
    public MaterialAllInfoInventoryDTO clearQuantity() {
        this.fabricQuanlity = 0.0;
        return this;
    }

    public MaterialAllInfoInventoryDTO(ResultSet resultSet) throws SQLException {
        super(resultSet);
        this.fabricQuanlity = ValueUtil.DoubleNullValue(resultSet.getString("fabric_quanlity"));
        //  保留小数点后两位
        try {
            this.unitPrice =
                    ValueUtil.DoubleNullValue(
                            new DecimalFormat(".00").format(ValueUtil.DoubleNullValue(resultSet.getString("unit_price")))
                    );
        } catch (Exception e) {
            this.unitPrice = ValueUtil.DoubleNullValue(resultSet.getString("unit_price"));
        }
    }

    public MaterialAllInfoInventoryDTO(Map<String, Object> stringObjectMap) {
        super(stringObjectMap);
        this.fabricQuanlity = stringObjectMap.containsKey("fabric_quanlity") && (stringObjectMap.get("fabric_quanlity") != null)
                ? ValueUtil.DoubleNullValue(stringObjectMap.get("fabric_quanlity").toString())
                : 0.0;
        this.unitPrice = stringObjectMap.containsKey("unit_price") && (stringObjectMap.get("unit_price") != null)
                ? ValueUtil.DoubleNullValue(stringObjectMap.get("unit_price").toString())
                : 0.0;
        //  保留小数点后两位
        try {
            this.unitPrice =
                    ValueUtil.DoubleNullValue(
                            new DecimalFormat(".00").format(this.unitPrice)
                    );
        } catch (Exception ignored) {
        }
    }

    public MaterialAllInfoInventoryDTO(String id, String vendorName, String materialNo, String meterialDesc, String composition, String density, String weight, String width, String thick, String elastic, String feature, String continuity, String uasge, String inspectionResults, String color, String season, String style, String arrangementMethod, double fabricQuanlity, double unitPrice) {
        super(
                id, vendorName, materialNo, meterialDesc, composition, density, weight, width, thick, elastic, feature, continuity, uasge, inspectionResults, color, season, style, arrangementMethod
        );
        this.fabricQuanlity = fabricQuanlity;
        this.unitPrice = unitPrice;
    }

    public MaterialAllInfoDTO build() {
        return new MaterialAllInfoDTO(
                this.id,
                this.vendorName,
                this.materialNo,
                this.meterialDesc,
                this.composition,
                this.density,
                this.weight,
                this.width,
                this.thick,
                this.elastic,
                this.feature,
                this.continuity,
                this.uasge,
                this.inspectionResults,
                this.color,
                this.season,
                this.style,
                this.arrangementMethod
        );
    }
}
